<?php
/**
 * Created by PhpStorm
 * User: shen.zk
 * Date: 2023/8/27
 * Time: 11:34
 * Comment: ''
 */

namespace App\Repository;

use App\Models\MerchantsInfo;
use Illuminate\Support\Facades\DB;

class MerchantsInfoRepo extends BaseRepo
{
    /**
     * 根据创作者标识获取信息
     * @param string $access_mid
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null
     * @author shen.zk
     * @date 2023-08-27 11:50
     */
    public function getByMerchantIdRepo(string $access_mid)
    {
        return MerchantsInfo::query()->where('access_mid', $access_mid)->first();
    }

    /**
     * 根据商户号查询信息
     * @param string $access_mid
     * @return array
     * Author:xsc
     * Time:2023/9/5 13:59
     */
    public function getInfoByAccessMid(string $access_mid)
    {
        $fields = [
            'i.mer_id as merchants_id',
            'i.access_mid',
            'i.user_name',
            'i.id',
            'i.company',
            'i.user_name',
            'i.portrait',
            'i.company_int',
            'i.contact',
            'i.contact_phone',
            'i.assort_id',
            'i.assort_link',
            'i.is_multipart',
            'i.account_no as accsplit_mer_no',
            'i.multipart_rate',
            'i.mer_id',
            'i.create_time',
            'i.platform_id',
            'i.ca_address',
            'd.name',
            'c.remaining'
        ];
        $res = MerchantsInfo::query()
            ->from('merchants_info', 'i')
            ->join('merchants_admin as a', 'i.mer_id', '=', 'a.id','left')
            ->join('merchants_casting as c', 'c.mer_id', '=', 'a.id','left')
            ->join('assort as d', 'i.assort_id', '=', 'd.id','left')
            ->where('i.access_mid', $access_mid)
            ->first($fields);

        return $res ? $res->toArray() : [];
    }

    /**
     * 根据商户号查询平台
     * @param string $access_mid
     * @return array
     * Author:xsc
     * Time:2023/9/22 13:21
     */
    public function getPlatformByAccessMid(string $access_mid)
    {
        $fields = ['i.mer_id as merchants_id', 'i.access_mid', 'i.user_name', 'i.platform_id', 'p.platform_no'];
        $res = MerchantsInfo::query()
            ->from('merchants_info', 'i')
            ->leftJoin('open_platform as p', 'i.platform_id', '=', 'p.id')
            ->where('i.access_mid', $access_mid)
            ->first($fields);
        return $res ? $res->toArray() : [];
    }

    /**
     * 根据手机号获取创作者信息
     * @param string $phone
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null
     * @author shen.zk
     * @date 2023-10-23 15:44
     */
    public function getPhone(string $phone)
    {
        return MerchantsInfo::query()->where('contact_phone',$phone)->first();
    }

    /**
     * 保存创作者信息
     * @param array $data
     * @param int $id
     * @return int
     * @author shen.zk
     * @date 2023-10-23 17:44
     */
    public function save(array $data,int $id = 0)
    {
        if ($id) {
            return MerchantsInfo::query()->where('id', $id)->update($data);
        } else {
            return MerchantsInfo::query()->insertGetId($data);
        }
    }

    /**
     * 获取分渠道唯一标识
     * @param string $assort_link
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null
     * @author shen.zk
     * @date 2023-10-24 14:43
     */
    public function getAssortLink(string $assort_link)
    {
        return MerchantsInfo::query()->where('assort_link', $assort_link)->orderByDesc('id')->first();
    }

    /**
     * 根据接入商户号获取信息
     * @param string $access_id
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null
     * @author shen.zk
     * @date 2023-10-26 09:50
     */
    public function getByAccessIdFindInfo(string $access_id)
    {
        return MerchantsInfo::query()->where('access_mid', $access_id)->first();
    }
    /**
     * 根据商户号查询信息
     * @param string $access_mid
     * @return array
     * Author:xsc
     * Time:2023/10/12 18:09
     */
    public function getByAccessMid(string $access_mid)
    {
        $res = MerchantsInfo::query()->where('access_mid', $access_mid)->first();
        return $res ? $res->toArray() : [];
    }

    /**
     * 根据商户id查询信息
     * @param int $merchants_id
     * @return array
     * Author:xsc
     * Time:2023/10/25 17:19
     */
    public function getByMerchantsId(int $merchants_id)
    {
        $res = MerchantsInfo::query()->where('mer_id', $merchants_id)->first();
        return $res ? $res->toArray() : [];
    }

}
